import axios from 'axios';

// 配置axios实例
const api = axios.create({
  baseURL: 'http://localhost:8000/api/v1',

  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  }
});

// 仪表盘API
export const getDashboardData = () => api.get('/dashboard');

// 车位API
export const getParkingSpots = () => api.get('/parking/spots');
export const getParkingSummary = () => api.get('/parking/summary');

// 预约API
export const getReservations = (status) => {
  const params = status ? { status } : {};
  return api.get('/reservations', { params });
};
export const getReservation = (id) => api.get(`/reservations/${id}`);
export const createReservation = (data) => api.post('/reservations', data);
export const cancelReservation = (id) => api.post(`/reservations/${id}/cancel`);

// 车辆API
export const processVehicleEntry = (data) => {
  // 确保请求体的键名与后端API一致
  const payload = {
    plate_number: data.plate_number,
    spot_number: data.spot_number
  };
  return api.post('/vehicles/entry', payload);
};

export const processVehicleExit = (data) => {
  // 确保请求体的键名与后端API一致
  const payload = {
    plate_number: data.plate_number
  };
  return api.post('/vehicles/exit', payload);
};

export const getVehicleRecords = () => api.get('/vehicles/records');

// 收入API
export const getIncomeRecords = () => api.get('/income/records');
export const getTotalIncome = () => api.get('/income/total');
export const getUnpaidBills = () => api.get('/income/unpaid');
export const payBill = (id) => api.post(`/income/bills/${id}/pay`);

// 计费规则API
export const getPricingRules = () => api.get('/pricing/rules');
export const getActivePricingRule = () => api.get('/pricing/rules/active');
export const createPricingRule = (data) => api.post('/pricing/rules', data);
export const updatePricingRule = (id, data) => api.put(`/pricing/rules/${id}`, data);
export const activatePricingRule = (id) => api.post(`/pricing/rules/${id}/activate`);

// WebSocket连接
export const createWebSocketConnection = () => {
  const ws = new WebSocket('ws://localhost:8000/ws');
  
  ws.onopen = () => {
    console.log('WebSocket连接已建立');
    // 发送心跳消息
    setInterval(() => {
      if (ws.readyState === WebSocket.OPEN) {
        ws.send('ping');
      }
    }, 30000);
  };
  
  return ws;
};

export default api; 